import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { FlatCompat } from '@eslint/eslintrc';
import tseslint from 'typescript-eslint';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const compat = new FlatCompat({
  baseDirectory: __dirname,
});

/**
 * 将 eslint 8 的配置转换为 eslint 9 的 flat config
 */
const eslint8Config = compat.config({
  extends: ['next/core-web-vitals', 'next/typescript', 'prettier'],
});

const eslintConfig = tseslint.config(
  { files: ['**/*.{js,mjs,cjs,ts,jsx,tsx}'] },
  {
    ignores: [
      '*.d.ts',
      'node_modules/',
      'dist/',
      'build/',
      'public/',
      'coverage/',
      '.next/',
      '.vercel/',
      'generated/',
      'prisma/',
      'ecosystem.config.js',
    ],
  },
  eslint8Config,
  {
    // 通用规则
    rules: {
      'no-console': 'warn',
      'no-shadow': 'error',
      'react/no-unused-prop-types': 'error',
      'import/no-cycle': [
        'error',
        {
          maxDepth: 10,
          ignoreExternal: true,
        },
      ],
    },
  },
  {
    // 特定规则：for server-side
    files: [
      'src/lib/**/*.ts',
      'src/actions/**/*.ts',
      'src/app/api/**/*.ts',
      'src/middleware.ts',
      'src/instrumentation.ts',
    ],
    rules: {
      'no-console': 'off',
    },
  },
  {
    // 特定规则：for 通用公共组件
    files: ['src/components/**/*.ts', 'src/components/**/*.tsx'],
    rules: {
      '@typescript-eslint/no-explicit-any': 'off',
    },
  }
);

export default eslintConfig;
